package cake.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cake.db.MySqlDb;
import cake.bean.User;
//�û���ݿ���ʶ���
public class UserDao {
	private MySqlDb db=new MySqlDb();
	//ͨ��id��ȡ�û�����
	public User getUser(long id)
	{
		User user=null;
		String sql="select * from user where id="+id;
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				user=new User();
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setId(rs.getLong("id"));
				user.setIdcard(rs.getString("idcard"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			db.Close();
		}
		
		return user;
	}
	//ͨ���û���������ȡ�ͻ�����
	public User getUser(String username,String password)
	{
		User user=null;
		String sql="select * from user where username='"+username+"' and password='"+password+"'";
		
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				user=new User();
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setId(rs.getLong("id"));
				user.setIdcard(rs.getString("idcard"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		
		return user;
	}
	
	public User getUser(String username)
	{
		User user=null;
		String sql="select * from user where username='"+username+"'";
		
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				user=new User();
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setId(rs.getLong("id"));
				user.setIdcard(rs.getString("idcard"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		
		return user;
	}
	
	//��ҳ��ȡ�ͻ��б?��pageSizeΪ0ʱ��ʾ����ҳ
	public List<User> getUsers(int currPage,int pageSize,String where)
	{
		List<User> users=new ArrayList<User>();
		String sql="select * from user ";
		if(!where.equals(""))
		{
			sql+=" Where "+where;
		}
		if(pageSize!=0)
		{
			sql+=" limit "+((currPage-1)*pageSize)+","+pageSize;
		}
		
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				User user=new User();
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setId(rs.getLong("id"));
				user.setIdcard(rs.getString("idcard"));
				
				users.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		
		return users;
	}
	//�����ͻ�
	public boolean addUser(User user)
	{
		String sql="Insert into User(username,password,idcard) values('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getIdcard()+"')";
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
		
	}
	//�޸Ŀͻ�
	public boolean updateUser(User user)
	{
		User oldUser=getUser(user.getId());
		
		if(oldUser==null)
		{
			return false;
		}
		
		String sql="Update User set password='"+user.getPassword()+"',idcard='"+user.getIdcard()+"' where id="+user.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
	//ɾ��ͻ�
	public  boolean deleteUser(User user)
	{
		User oldUser=getUser(user.getId());
		
		if(oldUser==null)
		{
			return false;
		}
		
		String sql="Delete from User where id="+user.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
}
